package com.yanqu.road.dao.impl.activity.businessreunion;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.businessreunion.BusinessReunionBindData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ：zfr
 * @date ：2022/4/19 18:53
 */
public class BusinessReunionBindDaoImpl extends TempDataObjectDao<BusinessReunionBindData> {
    private static String getSql = "select * from t_u_reunion_bind where userId = ? and activityId = ?";

    public BusinessReunionBindData getBindData(long userId, int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getSql);
            call.setLong(1, userId);
            call.setInt(2, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String insertSql = "insert into t_u_reunion_bind(userId, activityId, bindRecallCode, addVipExp)" +
            "values(?,?,?,?)";

    @Override
    protected boolean addData(BusinessReunionBindData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getRecallCode()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getAddVipExp()));
        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "update t_u_reunion_bind set bindRecallCode = ?, addVipExp = ? where userId = ? and activityId = ?";
    @Override
    protected boolean updateData(BusinessReunionBindData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, data.getRecallCode()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getAddVipExp()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected BusinessReunionBindData parseData(ResultSet rs) throws SQLException {
        BusinessReunionBindData data = new BusinessReunionBindData();
        data.setUserId(rs.getLong("userId"));
        data.setActivityId(rs.getInt("activityId"));
        data.setRecallCode(rs.getString("bindRecallCode"));
        data.setAddVipExp(rs.getInt("addVipExp"));
        return data;
    }
}
